<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;
use think\helper\Hash;
/**
 * @title 权限管理
 * @description 接口说明
 */
class Role extends Api
{
    /**
     * @title 角色管理
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/role/index
     * @method POST
     * 
     * @return list:列表@
     * @list id:id username:姓名 avatar:头像 mobiles:手机 mobile:账号 position:职位 role:权限 addtime:添加时间
     * @return page:总页数
     */
    public function index()
    {
        $user = $this->user;
        $page = input('page')??1;
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];

        $list = db::name('admin_role')
        ->where($where)
        ->field('id,name,description,pid')
        ->page("$page,10")->select();
        $total = db::name('admin_role')->where($where)->count();
        foreach ($list as $k => $v) {
           $list[$k]['num'] = db::name('user')
           ->where('hid',$user['hid'])->where('role',"in",$v['id'])->count();
        }
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    } 
    /**
     * @title 角色编辑
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/role/index_edit
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     * @return list:列表@
     * @list id:id name:名称 description:描述 menu_auth:权限 
     * @return page:总页数
     */
    public function index_edit()
    {
        $user = $this->user;
        $id = input('id');
        $user = db::name('admin_role')->where('id',$id)->field('id,name,description,menu_auth')->find();
        $list['user'] = $user;
        return $this->ajaxReturn(0,'成功',$list);
    } 

     /**
     * @title 获取权限
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/role/role
     * @method POST
     * 
     */
    public function role()
    {
        $user = $this->user;
        $where[] = ['type','=',2];
        $menu = db::name('admin_menu')->where($where)->field('id,title,pid')->select();
        $menu = $this->tree($menu);
        $menus = db::name('admin_menu')->where('type',1)->field('id,title,pid')->select();
        $menus = $this->tree($menus);
        // print_R($menus);die;
        $list['menu'] = $menu;
        $list['menus'] = $menus;
        return $this->ajaxReturn(0,'成功',$list);
    }
    public function tree($array, $pid=0)
    {
        $tree = array();
        foreach ($array as $key => $value) {
            if ($value['pid'] == $pid) {
                $value['child'] = $this->tree($array, $value['id']);
                if (!$value['child']) {
                    unset($value['child']);
                }
                $tree[] = $value;
            }
        }
        return $tree;
    }
    /**
     * @title 删除角色
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/role/dels
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     */
    public function dels()
    {
        $user = $this->user;
        $id = input('id');
        $where[] = ['id','=',$id];
        if(db::name('admin_role')->where('id',$id)->update(['is_del'=>2])){
            return $this->ajaxReturn(0,'成功');
        }
    }

    /**
     * @title 权限添加
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/role/role_save
     * @method POST
     * @param name:id type:int require:1 default: other: desc:修改id
     * @param name:name type:int require:1 default: other: desc:角色名
     * @param name:desc type:int require:1 default: other: desc:角色描述
     * @param name:menu type:int require:1 default: other: desc:勾选权限["297","298"]
     */
    public function role_save()
    {

        $user = $this->user;
        $name = input('name');
        $desc = input('desc');
        $menu = input('menu');
        $id = input('id');

        if(!$name || !$desc || !$menu){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if($id){
            $data['name'] = $name;
            $data['description'] = $desc;
            $data['menu_auth'] = $menu;
            if(db::name('admin_role')->where('id',$id)->update($data)){
                return $this->ajaxReturn(0,'成功');
            }
        }else{
            $hid = $user['hid'];
            $pid = db::name('admin_role')->where('hid',$hid)->where('pid',0)->value('id');
            $data['pid'] = $pid;
            $data['name'] = $name;
            $data['description'] = $desc;
            $data['menu_auth'] = $menu;
            $data['sort'] = 100;
            $data['create_time'] = time();
            $data['status'] = 1;
            $data['access'] = 1;
            $data['hid'] = $hid;
            $data['accessa'] = 1;
            $data['default_module'] = 1;
            if(db::name('admin_role')->insert($data)){
                return $this->ajaxReturn(0,'成功');
            }
        }
    }

}